package com.life.util;


import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultValueProcessor;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;

public class JsonConfigDate extends JsonConfig{

 public JsonConfigDate() {
	/* 先调父构造函数、然后做个性化处理 */
	super();

	/* 对日期类型属性做格式化处理 */
	super.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {
		private SimpleDateFormat sdfLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		private SimpleDateFormat sdfShort = new SimpleDateFormat("yyyy-MM-dd");

		
		
		public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			String[] obj = {};
			if (value == null)
				return "";
			if (value instanceof Date[]) {
				Date[] dates = (Date[]) value;
				obj = new String[dates.length];
				for (int i = 0; i < dates.length; i++) {
					obj[i] = sdfLong.format(dates[i]);
				}
			}
			return obj;
		}

		public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			// System.out.println("-----key: "+key+"    value:"+value);
			if (value == null)
				return "";
			if (value instanceof Date) {
				if (key.equals("dateCreate")||key.equals("updtDate")||key.equals("rgstDate")||key.equals("rgstTime")) {
					return sdfLong.format(value);
				} else {
					return sdfShort.format(value);
				}
			}
			return value.toString();
		}
	});

	// 包含TRUE,排除FALSE
	super.setJavaPropertyFilter(new PropertyFilter() {
		public boolean apply(Object source, String name, Object value) {
			// System.out.println("-----: "+name+":"+value);
			if (name.equals("check")) {
				return true;
			}
			return false;
		}
	});
	
	super.registerDefaultValueProcessor(Integer.class, new DefaultValueProcessor(){
		public Object getDefaultValue(Class type) {     
            return null;     
        } 
	});
	
	super.registerDefaultValueProcessor(Double.class, new DefaultValueProcessor(){
		public Object getDefaultValue(Class type) {     
            return null;     
        } 
	});
 }
 
 public JsonConfigDate(String[] ignoreFiled){
	 new JsonConfigDate();
	 super.setExcludes(ignoreFiled);
 }
}
